package com.i2silly.wxbot.config;

import com.i2silly.wxbot.constant.WxRobotConstant;
import com.i2silly.wxbot.entity.BotChatroom;
import com.i2silly.wxbot.entity.BotContact;
import com.i2silly.wxbot.pojo.WsReceive;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: wx-bot
 * @description：临时缓存
 * @author: pzhongqiang@linewell.com
 * @date: 2020年11月28日 14:42:19
 */
@Component
public class TempCache {
    Map<String,Object> tempCache = new HashMap<>();

    public int getSize() {
        return tempCache.size();
    }

    public Set<String> getKeys () {
        return tempCache.keySet();
    }

    public void addCache(String key, Object val) {
        tempCache.put(key, val);
    }

    public Object getVal(String key) {
        return tempCache.get(key);
    }

    public boolean getInit(String key) {
        return (boolean) tempCache.get(key);
    }

    @SuppressWarnings("unchecked")
    public Map<String, BotContact> getContacts() {
        return (Map<String, BotContact>) tempCache.get(WxRobotConstant.CONTACT_CACHE);
    }

    @SuppressWarnings("unchecked")
    public Map<String, List<BotChatroom>> getChatrooms() {
        return (Map<String, List<BotChatroom>>) tempCache.get(WxRobotConstant.CHATROOM_CACHE);
    }

    public Set<String> getChatroomIds() {
        return getChatrooms().keySet();
    }

    @SuppressWarnings("unchecked")
    public Set<String> getSetCache(String key) {
        return (Set<String>) tempCache.get(key);
    }

    @SuppressWarnings("unchecked")
    public Map<String, WsReceive> getAllLastWsReceive() {
        return (Map<String, WsReceive>)tempCache.get(WxRobotConstant.LAST_RECEIVE_MSG);
    }

    @SuppressWarnings("unchecked")
    public WsReceive getLastWsReceive(String key) {
        Map<String, WsReceive> map = (Map<String, WsReceive>)tempCache.get(WxRobotConstant.LAST_RECEIVE_MSG);
        return map.get(key);
    }

    @SuppressWarnings("unchecked")
    public void addLastWsReceive(String key, WsReceive wsReceive) {
        Map<String, WsReceive> map = (Map<String, WsReceive>)tempCache.get(WxRobotConstant.LAST_RECEIVE_MSG);
        map.put(key, wsReceive);
        addCache(WxRobotConstant.LAST_RECEIVE_MSG, map);
    }
}
